/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JINSCDemoUI.java
 *
 * Created on 18 sept. 2010, 18:41:40
 */

package fabricelaborde.jinsc.jinscdemoui;

import fabricelaborde.jinsc.jinscapi.JINSCApi;
import fabricelaborde.jinsc.jinscapi.JINSCException;
import fabricelaborde.jinsc.jinscapi.ValueKey;
import javax.swing.JOptionPane;

/**
 *
 * @author TOSHIBA
 */
public class JINSCDemoUI extends javax.swing.JFrame {

    /** Creates new form JINSCDemoUI */
    public JINSCDemoUI() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextFieldFirstName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jFormattedTextFieldBirthDate = new javax.swing.JFormattedTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldNIR = new javax.swing.JTextField();
        jToggleButtonINSC = new javax.swing.JToggleButton();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldINSC = new javax.swing.JTextField();
        jTextFieldINSCKey = new javax.swing.JTextField();
        jTextFieldNIRKey = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Prénom : ");

        jTextFieldFirstName.setText("Catherine");

        jLabel2.setText("Date de Naissance : ");

        jFormattedTextFieldBirthDate.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("yyMMdd"))));
        jFormattedTextFieldBirthDate.setText("650123");

        jLabel3.setText("NIR : ");

        jTextFieldNIR.setText("1650177851044");
        jTextFieldNIR.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldNIRFocusLost(evt);
            }
        });

        jToggleButtonINSC.setText("Calculer l'INS-C");
        jToggleButtonINSC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButtonINSCActionPerformed(evt);
            }
        });

        jLabel4.setText("INS-C :");

        jTextFieldINSC.setEditable(false);
        jTextFieldINSC.setFont(new java.awt.Font("Tahoma", 3, 11));

        jTextFieldINSCKey.setEditable(false);
        jTextFieldINSCKey.setFont(new java.awt.Font("Tahoma", 3, 11));

        jTextFieldNIRKey.setText("71");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldFirstName, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jFormattedTextFieldBirthDate, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextFieldNIR, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldNIRKey, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jTextFieldINSC))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(58, 58, 58)
                                .addComponent(jToggleButtonINSC)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextFieldINSCKey, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE))
                    .addComponent(jLabel4))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jFormattedTextFieldBirthDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldNIR, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldNIRKey, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jToggleButtonINSC)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldINSC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldINSCKey, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jToggleButtonINSCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButtonINSCActionPerformed
        String firstName = jTextFieldFirstName.getText() ;
        String birthDate = jFormattedTextFieldBirthDate.getText();
        ValueKey nir = new ValueKey(jTextFieldNIR.getText(), jTextFieldNIRKey.getText());

        jTextFieldINSC.setText("");
        jTextFieldINSCKey.setText("");

        ValueKey insc;
        try {
        
            insc = JINSCApi.getInstance().getINSC(firstName, birthDate, nir);
            jTextFieldINSC.setText(insc.getValue());
            jTextFieldINSCKey.setText(insc.getKey());
        } catch (JINSCException ex) {
            JOptionPane.showMessageDialog(this, "Erreur lors du calcul de l'INS-C : "+ex.getMessage());
        }
        
    }//GEN-LAST:event_jToggleButtonINSCActionPerformed

    private void jTextFieldNIRFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldNIRFocusLost
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldNIRFocusLost

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JINSCDemoUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField jFormattedTextFieldBirthDate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTextFieldFirstName;
    private javax.swing.JTextField jTextFieldINSC;
    private javax.swing.JTextField jTextFieldINSCKey;
    private javax.swing.JTextField jTextFieldNIR;
    private javax.swing.JTextField jTextFieldNIRKey;
    private javax.swing.JToggleButton jToggleButtonINSC;
    // End of variables declaration//GEN-END:variables

}
